package com.edusite

class CurriculumsController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {

        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [curriculumsInstanceList: Curriculums.list(params), curriculumsInstanceTotal: Curriculums.count()]
    }

    def create = {
	if (!session.user)
	   redirect(uri:"/")
	   
        def curriculumsInstance = new Curriculums()
        curriculumsInstance.properties = params
        
        return [curriculumsInstance: curriculumsInstance]
    }

    def save = {
        def curriculumsInstance = new Curriculums(params)
        def f = request.getFile('foto')
        def okcontents = ['image/png','image/jpeg','image/gif']
        if (!okcontents.contains(f.getContentType())) {
           flash.message =" La foto debe ser ${okcontents}"
           render (view:'create', model:[curriculumsInstance: curriculumsInstance])
           return
           }
        curriculumsInstance.foto=f.getBytes()
        curriculumsInstance.fotoTipo=f.getContentType()
  
        if (curriculumsInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), curriculumsInstance.id])}"
            redirect(action: "show", id: session.user.id)
        }
        else {
            render(view: "create", model: [curriculumsInstance: curriculumsInstance])
        }
    }

    def show = {
         Personas lapersona = Personas.get(params.id)
        def curriculumsInstance = Curriculums.findByPersona(lapersona)
        if (!curriculumsInstance) {

            if (session.user) {
//           flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"

                redirect(action:"create")
            } else {

                redirect(uri: "/home/indice")
            }
            
        }
        else {

        if (params.id.toString() != session.user.id.toString())
        { 
            if (lapersona.tipo != 'profesor') {
               redirect(uri: "/home/indice")
		}
	    else {
           [curriculumsInstance: curriculumsInstance]
		}
	}	
	 else {

            [curriculumsInstance: curriculumsInstance]
           }
        }
    }

    def showProfe = {
         Personas lapersona = Personas.get(params.id)
        def curriculumsInstance = Curriculums.findByPersona(lapersona)
        if (!curriculumsInstance) {

            if (session.user) {
//           flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"

                redirect(action:"create")
            } else {

                redirect(uri: "/home/indice")
            }
            
        }
        else {

        if (params.id.toString() != session.user.id.toString())
        { 
            if (lapersona.tipo != 'profesor') {
               redirect(uri: "/home/indice")
		}
	    else {
           [curriculumsInstance: curriculumsInstance]
		}
	}	
	 else {

            [curriculumsInstance: curriculumsInstance]
           }
        }
    }

    def showFoto ={
    
        def curriculumsInstance = Curriculums.get(params.id)
        if (!curriculumsInstance)

        
        response.setContentType(curriculumsInstance.fotoTipo)
        response.setContentLength(curriculumsInstance.foto.size())
        
        OutputStream out= response.getOutputStream();
        out.write(curriculumsInstance.foto);
        out.close();
    
    }

    def edit = {
        def curriculumsInstance = Curriculums.get(params.id)
        if (!curriculumsInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [curriculumsInstance: curriculumsInstance]
        }
    }

    def update = {
        def curriculumsInstance = Curriculums.get(params.id)
        if (curriculumsInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (curriculumsInstance.version > version) {
                    
                    curriculumsInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'curriculums.label', default: 'Curriculums')] as Object[], "Another user has updated this Curriculums while you were editing")
                    render(view: "edit", model: [curriculumsInstance: curriculumsInstance])
                    return
                }
            }
            curriculumsInstance.properties = params
            if (!curriculumsInstance.hasErrors() && curriculumsInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), curriculumsInstance.id])}"
                redirect(action: "show", id: curriculumsInstance.id)
            }
            else {
                render(view: "edit", model: [curriculumsInstance: curriculumsInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def curriculumsInstance = Curriculums.get(params.id)
        if (curriculumsInstance) {
            try {
                curriculumsInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'curriculums.label', default: 'Curriculums'), params.id])}"
            redirect(action: "list")
        }
    }
}
